package com.example.ai.utils;

import org.apache.commons.codec.binary.Base64;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;

/**
 * @author : zhayh
 * @date : 2021-4-24 19:49
 * @description :
 */

public class ImageTools {
    /**
     * 图片转Base64字符串
     * @param imgFile
     * @return
     */
    public static final String imageToBase64(MultipartFile imgFile) {
        byte[] data = null;
        try {
            InputStream is = imgFile.getInputStream();
            data = new byte[is.available()];
            is.read(data);
            is.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
//        BASE64Encoder encoder = new BASE64Encoder();
        return Base64.encodeBase64String(data);
    }
}
